package cn.tedu.ivos.dict.service.Impl;

import cn.tedu.ivos.dict.mapper.DictMapper;
import cn.tedu.ivos.dict.pojo.dto.DictSaveParam;
import cn.tedu.ivos.dict.pojo.dto.DictQuery;
import cn.tedu.ivos.dict.pojo.entity.Dict;
import cn.tedu.ivos.dict.pojo.vo.DictVO;
import cn.tedu.ivos.dict.service.DictService;
import cn.tedu.ivos.dictOption.mapper.DictOptionMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class DictServiceImpl implements DictService {
    @Autowired
    private DictMapper dictMapper;
    @Autowired
    private DictOptionMapper dictOptionMapper;
    @Override
    public List<DictVO> selectDict(DictQuery dictQuery) {
        List<DictVO> list = dictMapper.selectDict(dictQuery);
        return list;
    }

    @Override
    public int saveDict(DictSaveParam dictSaveParam) {
        log.debug("开始执行【保存字典】业务层方法，参数：{}", dictSaveParam);
        Dict dict = new Dict();
        BeanUtils.copyProperties(dictSaveParam,dict);
        if(dict.getId()==null){
            dict.setCreateTime(new Date());
            return dictMapper.insertDict(dict);
        }else{
            dict.setUpdateTime(new Date());
            return dictMapper.updateDict(dict);
        }
    }

    @Override
    public int deleteDict(Long id) {
        log.debug("开始执行【删除字典】业务层方法，参数：{}", id);
        dictOptionMapper.deleteByDictId(id);
        return dictMapper.deleteDict(id);
    }
}
